package com.cskaoyan.controller.back.ad;


import com.cskaoyan.bean.bo.back.ad.AdBo;
import com.cskaoyan.bean.po.BaseParam;
import com.cskaoyan.bean.po.MarketAd;
import com.cskaoyan.bean.vo.back.BaseRespVo;
import com.cskaoyan.bean.vo.back.ad.AdListVo;
import com.cskaoyan.exception.MarketValidationException;
import com.cskaoyan.service.back.ad.AdvertisingService;
import com.cskaoyan.util.ValidationUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;


/**
 * 广告控制器
 *
 * @author 李然
 * @date 2022/10/12
 */
@RestController
@RequestMapping("admin/ad")
public class AdvertisingController {

    @Autowired
    AdvertisingService advertisingService;

    /**
     * 列表
     *
     * @param listAdBo 列表广告博
     * @param name
     * @param content  内容
     * @return {@link BaseRespVo}
     *///广告筛选列表
    @RequiresPermissions("admin:ad:list")
    @GetMapping("list")
    public BaseRespVo list(BaseParam listAdBo, String name, String content) {

        AdListVo<MarketAd> adList = advertisingService.getAdList(listAdBo, name, content);

        return BaseRespVo.ok(adList);
    }

    /**
     * 删除
     *
     * @param ad 广告
     * @return {@link BaseRespVo}
     *///逻辑删除广告
    @RequiresPermissions("admin:ad:delete")
    @PostMapping("delete")
    public BaseRespVo delete(@RequestBody AdBo ad) {
        advertisingService.delete(ad);

        return BaseRespVo.ok();
    }

    /**
     * 更新
     *
     * @param ad     广告
     * @param result 结果
     * @return {@link BaseRespVo}
     * @throws MarketValidationException 市场验证异常
     */
    @RequiresPermissions("admin:ad:update")
    @PostMapping("update")
    public BaseRespVo update(@RequestBody @Valid AdBo ad, BindingResult result) throws MarketValidationException {

        ValidationUtils.valid(result);
        return BaseRespVo.ok(advertisingService.update(ad));
    }

    /**
     * 创建
     *
     * @param ad     广告
     * @param result 结果
     * @return {@link BaseRespVo}
     * @throws MarketValidationException 市场验证异常
     */
    @RequiresPermissions("admin:ad:create")
    @PostMapping("create")
    public BaseRespVo create(@RequestBody @Valid AdBo ad, BindingResult result) throws MarketValidationException {
        ValidationUtils.valid(result);
        MarketAd marketAd = advertisingService.create(ad);

        return BaseRespVo.ok(marketAd);

    }

}
